<template>
  <div class="audition158">
    <p>比如把数组<span class="color">[1, [2, 3, [4, 5]], 6]</span>转化为<span class="color">[1, 2, 3, 4, 5, 6]</span></p>
    <hr>
    <p>方法一：flat</p>
    <code class="code">
      console.log(arr.flat(Infinity))
    </code>
    <p>方法二：不使用api，用递归</p>
    <v-md-preview :text="text1" />
    <p>方法三：用递归+闭包</p>
    <v-md-preview :text="text2" />
  </div>
</template>

<script>
const prefix = '``` js'
const suffix = '```'
export default {
  name: 'Audition158',
  data() {
    return {
      text1: `${prefix}
      var arr1 = []
      function flat(arr) {
        arr.forEach(item => {
          Array.isArray(item) ? flat(item) : arr1.push(item)
        })
        return arr1
      }
      console.log(flat(arr)) // [1, 2, 3, 4, 5, 6]
      \n${suffix}`,
      text2: `${prefix}
      function flat() {
        var arr1 = []
        return function flat1(arr) {
          arr.forEach(item => {
            Array.isArray(item) ? flat1(item) : arr1.push(item)
          })
          return arr1
        }
      }
      console.log(flat()(arr)) // [1, 2, 3, 4, 5, 6]
      \n${suffix}`,
    }
  }
}
</script>

<style>

</style>